var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');

var options = {
    port: 994,
    host: 'smtp.163.com',
    secure: true,
    auth: {
        user: 'metrocloud@163.com',
        pass:'bzmsragvtyoxzuhd',
        pass1: 'getElementById()'
    }
};

var from = {
    name: '上勘院云监护平台',
    address: 'metrocloud@163.com'
};
// create reusable transporter object using SMTP transport

var dot = require('dot');
var path = require('path');
var fs = require('fs');


exports.notifyReviewer = function (data, emailAdress) {
    var origin = fs.readFileSync(path.join(__dirname, './mailTemplate/notifyReviewer.dot'));
    var render = dot.template(origin);
    var htmlTxt = render(data);

    var mailOptions = {
        from: from,
        to: emailAdress, // list of receivers
        subject: data.realName + '希望加入系统', // Subject line
        html: htmlTxt // html body
    };

    // send mail with defined transport object
    var transporter = nodemailer.createTransport(smtpTransport(options));
    transporter.sendMail(mailOptions, function (error, info) { });
};


exports.notifyApplier = function (data, emailAdress) {
    var origin = fs.readFileSync(path.join(__dirname, './mailTemplate/notifyApplier.dot'));
    var render = dot.template(origin);

    var action = data.accept ? "批准" : "拒绝";
    data.action = action;
    var htmlTxt = render(data);


    var mailOptions = {
        from: from,
        to: emailAdress, // list of receivers
        subject: data.reviewer + action + '了你的申请', // Subject line
        html: htmlTxt // html body
    };

    // send mail with defined transport object
    var transporter = nodemailer.createTransport(smtpTransport(options));
    transporter.sendMail(mailOptions, function (error, info) { });
};

exports.notifyDataReviewer = function (data, emailAdress) {
    var origin = fs.readFileSync(path.join(__dirname, './mailTemplate/notifyDataReviewer.dot'));
    var render = dot.template(origin);
    var htmlTxt = render(data);

    var mailOptions = {
        from: from,
        to: emailAdress, // list of receivers
        subject: data.prjName + "有新的数据",
        html: htmlTxt // html body
    };

    // send mail with defined transport object
    var transporter = nodemailer.createTransport(smtpTransport(options));
    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(error)
        } else {
            console.log(info);
        }
    });
};

exports.sendPassword = function (data, emailAddress) {
    var origin = fs.readFileSync(path.join(__dirname, './mailTemplate/sendPassword.dot'));
    var render = dot.template(origin);
    var htmlTxt = render(data);

    var mailOptions = {
        from: from,
        to: emailAddress, // list of receivers
        subject: "云图系统密码找回",
        html: htmlTxt // html body
    };

    // send mail with defined transport object
    var transporter = nodemailer.createTransport(smtpTransport(options));
    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log('send psw mail err:', error)
        } else {
            console.log(info);
        }
    });
};
